package edu.chen.bean;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "user_")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class User {
    private int id;
    private String username;
    private String password;
    private Set<Product> products;

    /**
     *ManyToMany的mappedBy属性指明另外一张表格的外键,且关系由另外一张表格维护
     */
    @ManyToMany(cascade = CascadeType.ALL,mappedBy = "users")
//    @JoinTable(name = "product_user",           //关联表格
//            joinColumns = {@JoinColumn(name = "uid")},  //本表格外键
//            inverseJoinColumns = {@JoinColumn(name = "pid")})   //另外表格的外键
    public Set<Product> getProducts() {
        return products;
    }

    public void setProducts(Set<Product> products) {
        this.products = products;
    }

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "password")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
